using System.Text;

namespace KL.Standard.Model.Infrastructure;

/// <summary>
    /// 查询载体
    /// </summary>
    public class PageSearchModel
    {
        public PageSearchModel()
        {
            
        }

        public PageSearchModel(Dictionary<string, string> param)
        {
            Limit = 10;
            Offset = 0;
            pageIndex = 1;
            if (param.ContainsKey("size"))
            {
                Limit = Int32.Parse(param["size"]);
                param.Remove("size");
            }
            if (param.ContainsKey("page"))
            {
                pageIndex = Int32.Parse(param["page"]);
                Offset = (Int32.Parse(param["page"]) - 1) * Limit;
                param.Remove("page");
            }
            if (param.ContainsKey("sort"))
            {
                Order = param["sort"];
                param.Remove("sort");
            }

            if (param.ContainsKey("order"))
            {
                Sort = param["order"];
                param.Remove("order");
            }
            if (param.Count > 0)
                searchParam = param;
            else {
                searchParam = new Dictionary<string, string>();
            }
        }
        
        public Dictionary<string, string> searchParam { set; get; }//查询条件
        public string Sort { get; set; }// 排序的字段
        public string Order { get; set; }// 升序还是降序
        public int Offset { get; set; }//从第几条数据开始显示
        public int Limit { get; set; }//每次显示的条数
        public int pageIndex { set; get; }//第几页
        /// <summary>
        /// 查询条件包含该元素同时该元素不能为空
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool ContainsParam(string key)
        {
            return this.searchParam.ContainsKey(key) && !string.IsNullOrEmpty(this.searchParam[key]);
        }
        /// <summary>
        /// 查询条件包含该元素同时该元素不能为空,或者 为0
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool ContainsIntParam(string key)
        {
            return this.searchParam.ContainsKey(key) && !string.IsNullOrEmpty(this.searchParam[key]) && this.searchParam[key].ToString() != "0";
        }
        public PageResult<T> ToPageResult<T>()
        {
            return new PageResult<T>()
            {
                pageIndex = pageIndex,
                pageSize = this.Limit,
            };
        }
    }
    
    /// <summary>
    /// 栏位过滤器
    /// </summary>
    public class ClmFilter
    {
        public string title { get; set; }
        public string prop { get; set; }
        /// <summary>
        /// 操作符号
        /// </summary>
        public string opt { get; set; }="like";
        public string val { set; get; }
    }
    
    /// <summary>
    /// 查询载体(新增过滤器
    /// </summary>
    public class PageSearchModel2
    {
        
        /// <summary>
        ///构建查询条件
        /// </summary>
        public void CreateSeachInfo()
        {
            Limit = 10;
            Offset = 0;
            pageIndex = 1;
            if (searchParam.ContainsKey("size"))
            {
                Limit = Int32.Parse(searchParam["size"]);
                searchParam.Remove("size");
            }
            if (searchParam.ContainsKey("page"))
            {
                pageIndex = Int32.Parse(searchParam["page"]);
                Offset = (Int32.Parse(searchParam["page"]) - 1) * Limit;
                searchParam.Remove("page");
            }
            if (searchParam.ContainsKey("sort"))
            {
                Order = searchParam["sort"];
                searchParam.Remove("sort");
            }

            if (searchParam.ContainsKey("order"))
            {
                Sort = searchParam["order"];
                searchParam.Remove("order");
            }

            
        }
        /// <summary>
        /// 创建sql
        /// </summary>
        /// <returns></returns>
        public string CreateFilterSQL()
        {
            if (clmFilters != null && clmFilters.Count > 0)
            {
                var sb = new StringBuilder();
                foreach (var filter in clmFilters)
                {
                    if (filter.opt.Equals("like"))
                        sb.Append($@" and {filter.prop} like '%'+@{filter.prop}+'%' ");
                    if (filter.opt.Equals("equ"))
                        sb.Append($@" and {filter.prop} = @{filter.prop} ");
                }

                return sb.ToString();
            }

            return string.Empty;
        }

        public List<ClmFilter> clmFilters { set; get; }    // 栏位过滤器
        public Dictionary<string, string> searchParam { set; get; }//查询条件
        public string Sort { get; set; }// 排序的字段
        public string Order { get; set; }// 升序还是降序
        public int Offset { get; set; }//从第几条数据开始显示
        public int Limit { get; set; }//每次显示的条数
        public int pageIndex { set; get; }//第几页
        /// <summary>
        /// 查询条件包含该元素同时该元素不能为空
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool ContainsParam(string key)
        {
            return this.searchParam.ContainsKey(key) && !string.IsNullOrEmpty(this.searchParam[key]);
        }
        /// <summary>
        /// 查询条件包含该元素同时该元素不能为空,或者 为0
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool ContainsIntParam(string key)
        {
            return this.searchParam.ContainsKey(key) && !string.IsNullOrEmpty(this.searchParam[key]) && this.searchParam[key].ToString() != "0";
        }
    }